﻿using AutoMapper;
using HongYeSystemWebApi.Templet.Entity.Entities;
using HongYeSystemWebApi.Templet.Entity.Request;
using HongYeSystemWebApi.Templet.Entity.Request.SysBankinfo;
using HongYeSystemWebApi.Templet.Entity.Request.SysProject;
using HongYeSystemWebApi.Templet.Entity.Response;
using HongYeSystemWebApi.Templet.Entity.Response.Project;
using HongYeSystemWebApi.Templet.Entity.Response.SysBankinfo;
using HongYeSystemWebApi.Templet.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HongYeSystemWebApi.Templet.Service
{
    public class BankinfoService : IBankinfoService
    {

        private readonly SqlSugarClient _db;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="db"></param>
        public BankinfoService(IHelpDB db, IMapper mapper)
        {
            this._db = db.OpenDB();
            _mapper = mapper;
        }
        public async Task DeleteAsync(MultiIdRequest request)
        {
            List<Bankinfo> DelData = new List<Bankinfo>();
            foreach (var item in request.Ids)
            {
                var project = await _db.Queryable<Bankinfo>().FirstAsync(a => a.Id == item);
                if (project==null)
                {
                    throw new Exception("请传递正确的项目Id号");
                }
                project.Delete = true;
                project.OperatDate = DateTime.Now;
                DelData.Add(project);
            }
           await _db.Updateable(DelData).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }

        public async Task InsertAsync(SysBankinfoInsertRequest request)
        {
            if (request.BankType==1 && await _db.Queryable<Bankinfo>().FirstAsync(x => x.BankType == 1 && !x.Delete) != null )  
            {
                throw new Exception("当前账户下以添加现金初始余额，无法再次添加");
            }
            var firData = await _db.Queryable<Bankinfo>().FirstAsync(x => x.BankNumber == request.BankNumber && !x.Delete);
            if (firData != null)
            {
                throw new Exception("当前银行卡编号以添加  请确认后重新提交");
            }
            var data = _mapper.Map<Bankinfo>(request);
            data.OperatDate = DateTime.Now;
            await _db.Insertable(data).ExecuteReturnIdentityAsync();
        }

        public async Task UpdateAsync(SysBankinfoUpdataRequest request)
        {
            var data = await _db.Queryable<Bankinfo>().FirstAsync(X => X.Id == request.Id);
            if (data == null)
            {
                throw new Exception("未找到修改原始数据");
            }
            var model = _mapper.Map<Bankinfo>(request);
            model.OperatDate = DateTime.Now;
            await _db.Updateable(model).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }
        public async Task<PageModel<SysBankinfoPageResponse>> GetListAsync(KeyPageRequest request)
        {
            var data = _db.Queryable<Bankinfo>().Where(x=> !x.Delete);
            if (string.IsNullOrEmpty(request.key))
            {
                data = data.Where(a => (a.BankName.Contains(request.key) || a.BankNumber.Contains(request.key)));
            }
            RefAsync<int> Total = data.Count();
            var resultpage = await data.ToPageListAsync(request.PageNumber, request.PageSize, Total);
            var result = _mapper.Map<List<SysBankinfoPageResponse>>(resultpage);
            return new PageModel<SysBankinfoPageResponse>() { Data = result, Total = Total };
        }
    }
}
